package com.example.qfsx;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class HomeActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        ViewPager2 viewPager = findViewById(R.id.viewPager);

        int[] imageUrls = {
                R.drawable.swiper7,
                R.drawable.swiper6,
                R.drawable.swiper5
        };
        ImageSliderAdapter adapter = new ImageSliderAdapter(imageUrls);
        viewPager.setAdapter(adapter);

        //金刚区
        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 4));

        List<IconItem> iconItems = new ArrayList<>();
        iconItems.add(new IconItem(R.drawable.icon1, "美妆个护"));
        iconItems.add(new IconItem(R.drawable.icon2, "母婴用品"));
        iconItems.add(new IconItem(R.drawable.icon3, "潮流服饰"));
        iconItems.add(new IconItem(R.drawable.icon4, "跨境购"));
        iconItems.add(new IconItem(R.drawable.icon5, "手机数码"));
        iconItems.add(new IconItem(R.drawable.icon6, "居家日用"));
        iconItems.add(new IconItem(R.drawable.icon7, "玩具乐器"));
        iconItems.add(new IconItem(R.drawable.icon8, "更多"));

        IconAdapter iconAdapter = new IconAdapter(iconItems);
        recyclerView.setAdapter(iconAdapter);


        RecyclerView recyclerGoodView = findViewById(R.id.recycler_good_view);
        recyclerGoodView.setLayoutManager(new GridLayoutManager(this, 2));

        List<PopularGood> productList = new ArrayList<>();

        List<String> defaultGoodsImgs2 = new ArrayList<>(); // 默认空列表
        defaultGoodsImgs2.add("https://example.com/default_image.jpg"); // 可选择性地添加一个默认URL

        productList.add(new PopularGood(
                "",             // tags
                0,              // cateId
                0,              // goodsId
                "默认商品",      // goodsName
                "",             // goodsNameStyle
                0,              // goodsNumber
                "https://example.com/default_image.jpg", // goodsImg
                "",             // goodsImgs
                0.0,            // marketPrice
                0.0,            // shopPrice
                0,              // isDelete
                0,              // isBest
                0,              // isNew
                0,              // isHot
                1,              // isSell
                "0",            // createdAt
                "0",            // updatedAt
                0,              // totalSell
                "",             // info
                0.0,            // latitude
                0.0,            // longitude
                defaultGoodsImgs2 // goodsImgs2
        ));
        ProductAdapter productAdapter = new ProductAdapter(productList);
        recyclerGoodView.setAdapter(productAdapter);

        getSwiperApi();
    }

    public void getSwiperApi() {
        Context _this = this;

        // 创建一个okhttp的对象
        OkHttpClient okHttpClient = new OkHttpClient();
        // 创建一个请求Request 请求地址1
        Request request = new Request.Builder()
                .url("http://123.56.141.187:8001/goods/index?pagenum=1&pagesize=10")
                .build();
        // 正常请求都是异步的，但是这里并没有开启新的新的线程，为什么？
        // 因为okhttp通过newCall在内部帮你开启了一个线程，所以你不用再次开启线程了，也是非常的nice
        okHttpClient.newCall(request).enqueue(new Callback() {
            // 重写Callback的OnFailure方法，这里是失败所执行的函数
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
//                e.printStackTrace();
                // 这里代码是修改TextView的内容，这个内容在不在主线程修改，绝对会报错的
                // 所以这里使用runOnUiThread方法，将这个内容拉到主线程执行
                // 这里可不能叫箭头函数，人家叫：Lambda表达式
//                runOnUiThread(() -> contentTv.setText(e.getMessage()));
            }

            // 重写Callback的OnResponse方法，这里是成功的执行
            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) {
                // 判断是否成功
                if (response.isSuccessful()) {
                    // 定义一个变量
                    String responseBody = null;

                    try {
                        // 取出内容数据
                        assert response.body() != null;
                        responseBody = response.body().string();
                        Gson temp = new Gson();
                        ResponseType Response = temp.fromJson(responseBody, ResponseType.class);

                        List<PopularGood> productList = Response.list;

                        runOnUiThread(() -> {
                            RecyclerView recyclerGoodView = findViewById(R.id.recycler_good_view);
                            ProductAdapter productAdapter = new ProductAdapter(productList);
                            recyclerGoodView.setAdapter(productAdapter);
                        });

                    } catch (Exception e) {
                        // e.printStackTrace();
                        System.out.println("hs " + e);
                        System.out.println("qf " + e);
                        System.out.println("qf " + e);
                        System.out.println("qf " + e);
                    }
                }
            }
        });
    }
}
